package com.android.activity.adapter;

import static android.content.ContentValues.TAG;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.android.R;
import com.android.bean.Good;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: HP
 * @date: 2022/6/20
 */
public class StaggerAdapter extends RecyclerView.Adapter<StaggerAdapter.InnerHolder> {

    protected final ArrayList<Good> mData;
    Context context;
    private OnItemClickListener mOnItemClickListener;



    public StaggerAdapter(ArrayList<Good> mData, Context context) {
        Log.d("TAG", "StaggerAdapter: ");
        this.context=context;
        this.mData = mData;
    }
    public StaggerAdapter(List<Good> mData) {
        this.context=context;
        this.mData = (ArrayList<Good>) mData;
    }


    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //传入这个view是item的界面
        View view = View.inflate(parent.getContext(), R.layout.item_stagger, null);
        //创建InnerHolder
        return new InnerHolder(view);
    }

    //绑定holder，一般用来设置数据
    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder,  int position) {

        String strTvTitle;
        String strTvCount;
        String imageUrl;
        String uerName;
        holder.setmPosition(position);



        if (mData.get(position) != null){
            strTvTitle = mData.get(position).getContent();
            strTvCount = mData.get(position).getPrice().toString();
            uerName = mData.get(position).getUsername();

            holder.getTvTitle().setText(strTvTitle);
            holder.getTvCount().setText(strTvCount);
            holder.getmUserName().setText(uerName);

            if (mData.get(position).getImageUrlList().size() != 0){
                imageUrl = (String) mData.get(position).getImageUrlList().get(0);
                Glide.with(context)
                        .load(imageUrl)
//                    .skipMemoryCache(true)//开启内容缓存
//                    .diskCacheStrategy(DiskCacheStrategy.NONE)//设置磁盘缓存模式：
                        .into(holder.getIvGoods());
            }

        }
    }

    @Override
    public int getItemCount() {
        if (mData != null) {
            return mData.size();
        }
        return 0;
    }

    //用于监听
    public void setOnItemClickListener(OnItemClickListener listener) {
        //设置一个监听，一个回调的接口
        this.mOnItemClickListener = listener;

    }

    public interface OnItemClickListener{
        //条目
        void onItemClick(int position);
    }

    public class InnerHolder extends RecyclerView.ViewHolder {

        private int mPosition;
        private final ImageView mImage;
        private final TextView mContent;
        private final TextView mPrice;
        private final TextView mUserName;


        public InnerHolder(View itemView){
            super(itemView);

            //找到条目的空间
            mImage = itemView.findViewById(R.id.iv_image);
            mContent = itemView.findViewById(R.id.tv_content);
            mPrice = itemView.findViewById(R.id.tv_price);
            mUserName = itemView.findViewById(R.id.seller_name);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //监听
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(mPosition);
                    }
                }
            });

        }

        public TextView getmUserName() {return mUserName;}
        public TextView getTvTitle() {
            return mContent;
        }
        public TextView getTvCount() {
            return mPrice;
        }
        public ImageView getIvGoods() {
            return mImage;
        }


        public void setmPosition(int position){
            this.mPosition=position;
        }
    }
//    //实现瀑布流效果
//    private void showStagger() {
//        //准备布局管理器
//        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
//        //设置布局管理器的方向
//        layoutManager.setReverseLayout(false);
//        //设置布局管理器到RecyclerView中
//        mList.setLayoutManager(layoutManager);
//
//        //创造适配器
//        StaggerAdapter adapter = new StaggerAdapter(mData);
//        //设置适配器
//        mList.setAdapter(adapter);
//
//    }
}
